<?php
namespace app\model;

use think\Model;

class Permissions extends Model
{
    protected $name = 'permissions';

    /**
     * 添加编辑权限
     */
    public function  addEditPermission($param =[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $name = $param['name'] ?? '';
        $parent_id= $param['parent_id'] ?? 0;
        $link = $param['link'] ?? '';
        $remark = $param['remark'] ?? '';
        $icon= $param['icon'] ?? '';
        $id = $param['id'] ?? '';
        if (empty($name)){
            return getFailResponse('参数有误');
        }
        $data =[
            'name'=>$name,
            'parent_id'=>$parent_id,
            'link'=>$link,
            'remark'=>$remark,
            'icon'=>$icon,
            'create_time'=>time(),
            'update_time'=>time(),
        ];
        if (empty($id)){
            self::create($data);
        }else{
            self::where(['id'=>$id])->update($data);
        }
        return getSuccessResponse();
    }

    /**
     * 权限详情
     */
    public function  permissionDetail($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $detail = self::where(['id'=>$id])->find();
        return getSuccessResponse('查询成功',$detail);
    }

    /**
     * 删除权限
     */
    public function  delPermission($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['ids'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $idsArr = explode(',',$id);
        foreach ($idsArr as $val){
            self::where(['id'=>$val])->update(['is_del'=>1,'update_time'=>time()]);
        }
        return getSuccessResponse('删除成功');
    }

    /**
     * 权限列表
     */
    public function  permissionList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $permissionList=[];
        $list = self::where(['is_del'=>0])->select();
        if (!empty($list)){
            $permissionList = listCategories($list->toArray());
        }
        $result =[
            'list'=>$permissionList
        ];
        return getSuccessResponse('查询成功',$result);
    }

    /**
     * 获取登陆用户权限列表
     */
    public function  loginAdminPermission($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $permissionList=[];
        $adminInfo = Admin::where(['id'=>$param['admin_id']])->find();
        if (empty($adminInfo)){
            return getFailResponse('管理员不存在');
        }
        if ($adminInfo['type'] ==1){
            $list = self::where(['is_del'=>0])->select();
            if (!empty($list)){
                $permissionList = listCategories($list->toArray());
            }
        }else{
            $adminRole = AdminRoles::where(['admin_id'=>$param['admin_id']])->select();
            if (!empty($adminRole)){
               $roleIds = array_column($adminRole->toArray(),'role_id');
               $rolePermissions = RolePermissions::where('role_id','in',$roleIds)->select();
               if(!empty($rolePermissions)){
                   $perIds = array_unique(array_column($rolePermissions->toArray(),'permission_id'));
                   $list = self::where(['is_del'=>0])->where('id','in',$perIds)->select();
                   if (!empty($list)){
                       $permissionList = listCategories($list->toArray());
                   }
               }
            }
        }
        $result =[
            'list'=>$permissionList
        ];
        return getSuccessResponse('查询成功',$result);
    }
    
    /**
     * 获取角色权限列表
     */
    public function  rolePermissionList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $roleInfo = Roles::where(['id'=>$id])->find();
        if (empty($roleInfo)){
            return getFailResponse('角色不存在');
        }
        $permissionList=[];
        $list = Permissions::where(['is_del'=>0])->select();
        if (!empty($list)){
            $list = $list->toArray();
            $rolePers = RolePermissions::where(['role_id'=>$id])->select();
            if (!empty($rolePers)){
                $perIds =array_column($rolePers->toArray(),'permission_id');
                foreach ($list as $key=>&$val){
                    if (in_array($val['id'],$perIds)){
                        $val['is_choose'] =1;  //1是被选中
                    }else{
                        $val['is_choose'] =0;  //0是没有被选中
                    }
                }
                $permissionList = listCategories($list);
            }
        }
        $result = [
            'list'=>$permissionList
        ];
        return getSuccessResponse('查询成功',$result);





    }




}